package com.rokyinfo.pdm.controller.v1;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.rokyinfo.pdm.controller.BasicPath;
import com.rokyinfo.pdm.entity.DBluetoothInfo;
import com.rokyinfo.pdm.service.IDBluetoothInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * <p>
 * 蓝牙信息表 前端控制器
 * </p>
 *
 * @author YuanZhiJian
 * @since 2017-01-21
 */
@Api(value = "/bluetoothInfo", tags = "BluetoothInfo", description = "蓝牙模块扩展信息")
@Controller
@RequestMapping(BasicPath.V1 + "/bluetoothInfo")
public class DBluetoothInfoController {
    @Autowired
    private IDBluetoothInfoService mIDBluetoothInfoService;

    @ApiOperation(value = "获取蓝牙模块扩展信息列表", notes = "")
    @RequestMapping(value = "", method = RequestMethod.GET)
    public ResponseEntity<Page<DBluetoothInfo>> getDBluetoothInfoList(@RequestParam(value = "offset", required = false, defaultValue = "1") Integer offset, @RequestParam(value = "limit", required = false, defaultValue = "20") Integer limit) throws Exception {
        // 还可以通过@RequestParam从页面中传递参数来进行查询条件或者翻页信息的传递
        EntityWrapper<DBluetoothInfo> wrapper = new EntityWrapper<DBluetoothInfo>();
        return ResponseEntity.ok(mIDBluetoothInfoService.selectPage(new Page<DBluetoothInfo>(offset, limit), wrapper));
    }

    @ApiOperation(value = "新建蓝牙模块扩展信息", notes = "根据DBluetoothInfo对象创建蓝牙模块扩展信息")
    @ApiImplicitParam(name = "DBluetoothInfo", value = "蓝牙模块扩展信息实体DBluetoothInfo", required = true, dataType = "DBluetoothInfo")
    @RequestMapping(value = "/", method = RequestMethod.POST)
    public ResponseEntity postDBluetoothInfo(@RequestBody DBluetoothInfo DBluetoothInfo) {
        // 除了@ModelAttribute绑定参数之外，还可以通过@RequestParam从页面中传递参数
        if (mIDBluetoothInfoService.insert(DBluetoothInfo)) {
            return new ResponseEntity(HttpStatus.CREATED);
        } else {
            return ResponseEntity.unprocessableEntity().build();
        }
    }


    @ApiOperation(value = "获取蓝牙模块扩展信息信息", notes = "根据url的SN来获取蓝牙模块扩展信息信息")
    @ApiImplicitParam(name = "sn", value = "设备SN", required = true, dataType = "String", paramType = "path")
    @RequestMapping(value = "/{sn}", method = RequestMethod.GET)
    public ResponseEntity<DBluetoothInfo> getDBluetoothInfo(@PathVariable String sn) {
        EntityWrapper<DBluetoothInfo> wrapper = new EntityWrapper<DBluetoothInfo>();
        DBluetoothInfo _mDBluetoothInfo = new DBluetoothInfo();
        _mDBluetoothInfo.setSn(sn);
        wrapper.setEntity(_mDBluetoothInfo);
        DBluetoothInfo mDBluetoothInfo = mIDBluetoothInfoService.selectOne(wrapper);
        if (mDBluetoothInfo != null) {
            return ResponseEntity.ok(mDBluetoothInfo);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @ApiOperation(value = "更新蓝牙模块扩展信息信息", notes = "根据url的SN来指定更新对象，并根据传过来的DBluetoothInfo信息来更新蓝牙模块扩展信息信息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sn", value = "设备SN", required = true, dataType = "String", paramType = "path"),
            @ApiImplicitParam(name = "DBluetoothInfo", value = "蓝牙模块扩展信息实体DBluetoothInfo", required = true, dataType = "DBluetoothInfo")
    })
    @RequestMapping(value = "/{sn}", method = RequestMethod.PUT)
    public ResponseEntity putDBluetoothInfo(@PathVariable String sn, @RequestBody DBluetoothInfo DBluetoothInfo) {
        EntityWrapper<DBluetoothInfo> wrapper = new EntityWrapper<DBluetoothInfo>();
        DBluetoothInfo.setSn(sn);
        wrapper.setEntity(DBluetoothInfo);
        if (mIDBluetoothInfoService.update(DBluetoothInfo, wrapper)) {
            return ResponseEntity.ok().build();
        } else {
            return ResponseEntity.unprocessableEntity().build();
        }
    }


    @ApiOperation(value = "删除蓝牙模块扩展信息", notes = "根据url的id来指定删除对象")
    @ApiImplicitParam(name = "sn", value = "设备SN", required = true, dataType = "String", paramType = "path")
    @RequestMapping(value = "/{sn}", method = RequestMethod.DELETE)
    public ResponseEntity deleteDBluetoothInfo(@PathVariable String sn) {
        EntityWrapper<DBluetoothInfo> wrapper = new EntityWrapper<DBluetoothInfo>();
        DBluetoothInfo mDBluetoothInfo = new DBluetoothInfo();
        mDBluetoothInfo.setSn(sn);
        wrapper.setEntity(mDBluetoothInfo);
        if (mIDBluetoothInfoService.delete(wrapper)) {
            return ResponseEntity.ok().build();
        } else {
            return ResponseEntity.notFound().build();
        }
    }
}